import AsyncStorage from '@react-native-async-storage/async-storage';

class AsyncStorageHelper {

  static async init() {
   
  }

  // 保存字符串
  static async setString(key: string, value: string): Promise<void> {
    try {
      await AsyncStorage.setItem(key, value);
    } catch (error) {
      console.error('Error saving string:', error);
    }
  }

  // 获取字符串
  static async getString(key: string): Promise<string | null> {
    try {
      return await AsyncStorage.getItem(key);
    } catch (error) {
      console.error('Error retrieving string:', error);
      return null;
    }
  }

  // 保存整数
  static async setInt(key: string, value: number): Promise<void> {
    await this.setString(key, value.toString()); // Store integers as strings
  }

  // 获取整数
  static async getInt(key: string): Promise<number | null> {
    const value = await this.getString(key);
    return value ? parseInt(value, 10) : null;
  }

  // 保存布尔值
  static async setBool(key: string, value: boolean): Promise<void> {
    await this.setString(key, value ? 'true' : 'false'); // Store booleans as strings
  }

  // 获取布尔值
  static async getBool(key: string): Promise<boolean | null> {
    const value = await this.getString(key);
    return value === 'true';
  }

  // 保存双精度浮点数
  static async setDouble(key: string, value: number): Promise<void> {
    await this.setString(key, value.toString()); // Store doubles as strings
  }

  // 获取双精度浮点数
  static async getDouble(key: string): Promise<number | null> {
    const value = await this.getString(key);
    return value ? parseFloat(value) : null;
  }

  // 保存字符串列表
  static async setStringList(key: string, value: string[]): Promise<void> {
    await this.setString(key, JSON.stringify(value)); // Store lists as JSON strings
  }

  // 获取字符串列表
  static async getStringList(key: string): Promise<string[] | null> {
    const value = await this.getString(key);
    return value ? JSON.parse(value) : null; // Parse JSON string back to array
  }

  // 移除指定的键值对
  static async remove(key: string): Promise<void> {
    try {
      await AsyncStorage.removeItem(key);
    } catch (error) {
      console.error('Error removing key:', error);
    }
  }

  // 清空所有数据
  static async clear(): Promise<void> {
    try {
      await AsyncStorage.clear();
    } catch (error) {
      console.error('Error clearing AsyncStorage:', error);
    }
  }
}

export default AsyncStorageHelper;